package com.molyfun.parents.activity.course;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.impl.IHorizontalTag;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.GsonUtil;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.view.crystalrangeseekbar.interfaces.OnRangeSeekbarChangeListener;
import com.molyfun.parents.view.crystalrangeseekbar.widgets.BubbleThumbRangeSeekbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 搜索家园标签列表页,点击后返回给发布页
 * 通过startActivityForResult调用
 */
public class CoursePickActivity extends BaseActivity implements View.OnClickListener, OnRangeSeekbarChangeListener {


    public static final String FLAG_SEARCH_TAG = "search_tag";

    public static final String FLAG_SEARCH_FROM = "search_from";

    public static final int FLAG_FROM_MAIN_TAB = 1001;

    public static final int FLAG_FROM_CATEGORY = 1002;

    private int mSearchFrom = FLAG_FROM_MAIN_TAB;

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.rangeBar)
    BubbleThumbRangeSeekbar mRangeBar;

    @InjectView(R.id.pick_min_text)
    TextView mPickMinText;
    @InjectView(R.id.pick_max_text)
    TextView mPickMaxText;

    @InjectView(R.id.submit)
    TextView mReset;
    @InjectView(R.id.base_title)
    TextView mTitle;

    @InjectView(R.id.save)
    TextView mSave;
    @InjectView(R.id.count_to_low)
    ImageView mCountToLow;
    @InjectView(R.id.count_to_high)
    ImageView mCountToHigh;
    @InjectView(R.id.evaluate_to_low)
    ImageView mEvaluateToLow;
    @InjectView(R.id.evaluate_to_high)
    ImageView mEvaluateToHigh;

    private CoursePickStorage mCoursePickStorage;

    public static class CoursePickStorage{
        private String minPrice = "0"; //最低价
        private String maxPrice = "3500"; //最高价
        private int mCountOrder = -1; //数量顺序 -1没有选 0由低到高 1由高到低
        private int mEvaluateOrder = -1; //评价顺序 -1没有选 0由低到高 1由高到低

        public String getMinPrice() {
            return minPrice;
        }

        public void setMinPrice(String minPrice) {
            this.minPrice = minPrice;
        }

        public String getMaxPrice() {
            return maxPrice;
        }

        public void setMaxPrice(String maxPrice) {
            this.maxPrice = maxPrice;
        }

        public int getmCountOrder() {
            return mCountOrder;
        }

        public void setmCountOrder(int mCountOrder) {
            this.mCountOrder = mCountOrder;
        }

        public int getmEvaluateOrder() {
            return mEvaluateOrder;
        }

        public void setmEvaluateOrder(int mEvaluateOrder) {
            this.mEvaluateOrder = mEvaluateOrder;
        }

        public List<IHorizontalTag> toHorizontalList(){
            List<IHorizontalTag> iHorizontalTags = new ArrayList<>();
            if(!(minPrice.equals("0") && maxPrice.equals("3500"))){
                iHorizontalTags.add(new CoursePickStr(minPrice + "~" + maxPrice));
            }
            if(mCountOrder != -1){
                if(mCountOrder == 0){
                    iHorizontalTags.add(new CoursePickStr("销量由低到高"));
                }else{
                    iHorizontalTags.add(new CoursePickStr("销量由高到低"));
                }
            }

            if(mEvaluateOrder != -1){
                if(mEvaluateOrder == 0){
                    iHorizontalTags.add(new CoursePickStr("评价顺序由低到高"));
                }else{
                    iHorizontalTags.add(new CoursePickStr("评价顺序由高到低"));
                }
            }

            return iHorizontalTags;
        }
    }

    public static class CoursePickStr implements IHorizontalTag{

        private String mStr;

        public CoursePickStr(String str){
            this.mStr = str;
        }

        @Override
        public String getName() {
            return mStr;
        }
    }

    public static final void startActivity(Context context, int searchFrom) {
        Intent intent = new Intent();
        intent.setClass(context, CoursePickActivity.class);
        intent.putExtra(FLAG_SEARCH_FROM, searchFrom);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_pick);
        getSupportActionBar().setTitle("");
        ButterKnife.inject(this);
        mBack.setOnClickListener(this);
        String coursePickStr = PreferencesStorageUtil.getCCoursePickSearchStr(CoursePickActivity.this);
        System.out.println("coursePickStr---->" + coursePickStr);
        if(!TextUtils.isEmpty(coursePickStr)){
            mCoursePickStorage = GsonUtil.fromJson(coursePickStr, CoursePickStorage.class);
        }else{
            mCoursePickStorage = new CoursePickStorage();
            mPickMaxText.setText("3500+");
        }
        mReset.setText("重置");
        mTitle.setText("筛选");

        mSearchFrom = getIntent().getIntExtra(FLAG_SEARCH_FROM, FLAG_FROM_MAIN_TAB);



        mRangeBar.setMaxStartValue(Float.parseFloat(mCoursePickStorage.getMaxPrice()));
        mRangeBar.setMinStartValue(Float.parseFloat(mCoursePickStorage.getMinPrice()));
//        mRangeBar.setNormalizedMinValue(Float.parseFloat(mCoursePickStorage.getMinPrice()));

//        mRangeBar.setNormalizedMinValue(Double.parseDouble(mCoursePickStorage.getMinPrice()));
//        mRangeBar.setNormalizedMaxValue(Double.parseDouble(mCoursePickStorage.getMaxPrice()));

        mRangeBar.setOnRangeSeekbarChangeListener(this);

        mPickMinText.setText(mCoursePickStorage.getMinPrice());

        if(mCoursePickStorage.getMaxPrice().equals("3500")) {
            mPickMaxText.setText("3500+");
        }else{
            mPickMaxText.setText(mCoursePickStorage.getMaxPrice());
        }

        if(mCoursePickStorage.getmCountOrder() != -1){
            if(mCoursePickStorage.getmCountOrder() == 0){
                mCountToHigh.setImageResource(R.drawable.bg_checkbox_question_selector);
                mCountToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
            }else{
                mCountToLow.setImageResource(R.drawable.bg_checkbox_question_selector);
                mCountToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
            }
        }else{
            mCountToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
            mCountToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
        }

        if(mCoursePickStorage.getmEvaluateOrder() != -1){
            if(mCoursePickStorage.getmEvaluateOrder() == 0){
                mEvaluateToHigh.setImageResource(R.drawable.bg_checkbox_question_selector);
                mEvaluateToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
            }else{
                mEvaluateToLow.setImageResource(R.drawable.bg_checkbox_question_selector);
                mEvaluateToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
            }
        }else{
            mEvaluateToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
            mEvaluateToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
        }

        mBack.setOnClickListener(this);
        mReset.setOnClickListener(this);
        mSave.setOnClickListener(this);
        mCountToLow.setOnClickListener(this);
        mCountToHigh.setOnClickListener(this);
        mEvaluateToLow.setOnClickListener(this);
        mEvaluateToHigh.setOnClickListener(this);
    }

    @Override
    public void valueChanged(Number minValue, Number maxValue) {
        mCoursePickStorage.setMinPrice(String.valueOf(minValue));
        mCoursePickStorage.setMaxPrice(String.valueOf(maxValue));
        mPickMinText.setText(String.valueOf(minValue));

        String maxValueStr = String.valueOf(maxValue);
        if(maxValueStr.equals("3500")){
            mPickMaxText.setText("3500+");
        }else{
            mPickMaxText.setText(String.valueOf(maxValue));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.count_to_low:
                mCountToLow.setImageResource(R.drawable.bg_checkbox_question_selector);
                mCountToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
                mCoursePickStorage.setmCountOrder(1);
                break;

            case R.id.count_to_high:
                mCountToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
                mCountToHigh.setImageResource(R.drawable.bg_checkbox_question_selector);
                mCoursePickStorage.setmCountOrder(0);
                break;

            case R.id.evaluate_to_low:
                mEvaluateToLow.setImageResource(R.drawable.bg_checkbox_question_selector);
                mEvaluateToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
                mCoursePickStorage.setmEvaluateOrder(1);
                break;

            case R.id.evaluate_to_high:
                mEvaluateToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
                mEvaluateToHigh.setImageResource(R.drawable.bg_checkbox_question_selector);
                mCoursePickStorage.setmEvaluateOrder(0);
                break;

            case R.id.submit:
                mCountToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
                mCountToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
                mEvaluateToLow.setImageResource(R.drawable.bg_checkbox_question_normal);
                mEvaluateToHigh.setImageResource(R.drawable.bg_checkbox_question_normal);
                mCoursePickStorage = new CoursePickStorage();

                mRangeBar.setNormalizedMinValue(Float.parseFloat(mCoursePickStorage.getMinPrice()));
                mRangeBar.setNormalizedMaxValue(Float.parseFloat(mCoursePickStorage.getMaxPrice()));
                mPickMinText.setText(mCoursePickStorage.getMinPrice());
                mPickMaxText.setText("3500+");
                break;
            case R.id.save:
                PreferencesStorageUtil.setCoursePickSearchStr(this, GsonUtil.toJson(mCoursePickStorage));
                Message uimessage = new Message();
                uimessage.what = EventDispatcherEnum.UI_EVENT_COURSE_PICK;
                uimessage.arg1 = mSearchFrom;
                MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);
//                if(mCoursePickDialogInterface != null){
//                    mCoursePickDialogInterface.onPick();
//                }
                finish();
                break;

        }
    }

}
